package com.yigang.homecontroller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yigang.entity.Collectionshoucang;
import com.yigang.entity.MyTheorder;
import com.yigang.entity.PayMent;
import com.yigang.entity.Shoppingcart;
import com.yigang.entity.ShoppingcartVO;
import com.yigang.entity.User;
import com.yigang.entity.UserAddress;
import com.yigang.service.CollectionService;
import com.yigang.service.MyTheorderService;
import com.yigang.service.ProductService;
import com.yigang.service.ShopingCartService;
import com.yigang.service.UserAddressService;
import com.yigang.utils.ResultUtil;
import com.yigang.utils.UserConst;

@Controller
@RequestMapping("my")
public class MyController {
	@Autowired
	ProductService productService;
	@Autowired
	ShopingCartService shopingCartService;
	@Autowired
	UserAddressService userAddressService;
	@Autowired
	MyTheorderService myTheorderService;
	@Autowired
	CollectionService collectionService;
	@RequestMapping("/Mycenter")
	public String center(Model model,Shoppingcart shopingCart, HttpServletRequest request, HttpSession session) {		
		Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject((String) attribute, User.class);
            if (user != null) {
                shopingCart.setId(user.getId() + "");
                List<ShoppingcartVO> list = shopingCartService.getAllShoppingcart(shopingCart);
                List<Map<String, Object>> clist = productService.productRecommend2();		
        		System.out.println(clist);
        		model.addAttribute("clist", clist);
                session.setAttribute("list", list);
                return "jsp/home/my";
                
            }
            return "jsp/userLogin";
        }
        return "jsp/userLogin";
    }
	@RequestMapping("/Myaddress")
	public String myaddress(Model model,UserAddress userAddress, HttpServletRequest request, HttpSession session) {		
		Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject((String) attribute, User.class);
            if (user != null) {
            	userAddress.setId(userAddress.getId() + "");
                List<UserAddress> list1 = userAddressService.getAllUserAddress(userAddress);
                List<UserAddress> list2 = userAddressService.getAllUserAddress1(userAddress);
        		
        		
                session.setAttribute("list1", list1);
                session.setAttribute("list2", list2);
                return "jsp/home/myaddress";
            }
            return "jsp/userLogin";
        }
        return "jsp/userLogin";
    }
	
	@RequestMapping("/MyTheorder")
	public String myorder(Model model, HttpServletRequest request, HttpSession session) {		
		Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject((String) attribute, User.class);
            if (user != null) {
            	
                List<Map<String, Object>> Alltheorder = myTheorderService.getAllTheorder();
                
        		
        		
                session.setAttribute("Alltheorder", Alltheorder);
             
                return "jsp/home/myorder";
            }
            return "jsp/userLogin";
        }
        return "jsp/userLogin";
    }
	@RequestMapping("/MyTheorder1")
	public String myorder1(Model model, HttpServletRequest request, HttpSession session) {		
		Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
		if (null != attribute) {
			User user = JSON.parseObject((String) attribute, User.class);
			if (user != null) {
				
				List<Map<String, Object>> Alltheorder1 = myTheorderService.getAllTheorder1();
				
				
				
				session.setAttribute("Alltheorder1", Alltheorder1);
				
				return "jsp/home/myorder1";
			}
			return "jsp/userLogin";
		}
		return "jsp/userLogin";
	}
	@RequestMapping("/MyTheorder2")
	public String myorder2(Model model, HttpServletRequest request, HttpSession session) {		
		Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
		if (null != attribute) {
			User user = JSON.parseObject((String) attribute, User.class);
			if (user != null) {
				
				List<Map<String, Object>> Alltheorder2 = myTheorderService.getAllTheorder2();
				
				
				
				session.setAttribute("Alltheorder2", Alltheorder2);
				
				return "jsp/home/myorder2";
			}
			return "jsp/userLogin";
		}
		return "jsp/userLogin";
	}
	 /*
     * 删除地址
     */
    @RequestMapping("/delAddress")
    @ResponseBody
    public ResultUtil delCart(UserAddress userAddress, HttpServletRequest request, HttpSession session) {
        int i = userAddressService.deleteByPrimaryKey(userAddress);
         if (i > 0) {
            return ResultUtil.ok(1);
        } else {
            return ResultUtil.error("删除失败");
        }
    }
    @RequestMapping("/updateorder")
    @ResponseBody
    public ResultUtil updateOrder(MyTheorder myorder, HttpServletRequest request, HttpSession session) {
    	int i = myTheorderService.update(myorder);
    	if (i > 0) {
    		return ResultUtil.ok(1);
    	} else {
    		return ResultUtil.error("删除失败");
    	}
    }
    @RequestMapping("/MyCollection")
	public String collection(Model model, HttpServletRequest request, HttpSession session) {		
		Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
		if (null != attribute) {
			User user = JSON.parseObject((String) attribute, User.class);
			if (user != null) {
				
				List<Map<String, Object>> collection = collectionService.getAllCollectionById();
				
				
				
				session.setAttribute("collection", collection);
				
				return "jsp/home/collection";
			}
			return "jsp/userLogin";
		}
		return "jsp/userLogin";
	}
    @RequestMapping("/addCollection")
    @ResponseBody
    public ResultUtil addCollection(Collectionshoucang collections, HttpServletRequest request, HttpSession session) {
        Object attribute = session.getAttribute(UserConst.LOGIN_USER_INFO);
        if (null != attribute) {
            User user = JSON.parseObject((String) attribute, User.class);
           
            
            
            int i = collectionService.addController(collections);
             if (i > 0) {
                return ResultUtil.ok(1);
            } else {
                return ResultUtil.error("订单创建失败");
            }
        } else {
            return ResultUtil.error("请先登录");
        }
    }
    /*
     * 删除收藏
     */
    @RequestMapping("/delCollection")
    @ResponseBody
    public ResultUtil delCollection(Collectionshoucang collections, HttpServletRequest request, HttpSession session) {
        int i = collectionService.deleteByPrimaryKey(collections);
         if (i > 0) {
            return ResultUtil.ok(1);
        } else {
            return ResultUtil.error("删除失败");
        }
    }
	}
	

